package com.chaoxuan.uebt.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.activity.WebActivity;
import com.chaoxuan.uebt.adapter.AdAdapter;
import com.chaoxuan.uebt.adapter.OnlineAdapter;
import com.chaoxuan.uebt.model.Ad;
import com.chaoxuan.uebt.model.Index;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.ActivityUtils;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.ICallBack;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.google.common.base.Splitter;
import java.util.List;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017/3/13.
 */
public class IndexFragment extends Fragment implements BaseQuickAdapter.OnItemClickListener,View.OnClickListener {
    String TAG="IndexFragment";
    AdAdapter adapter;
    OnlineAdapter onlineAdapter;
    TextView tvDigIncome,tvYesterdayDig,tvOnline,tvNet,tvToday,tvTime,btnDig;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, container, false);
        tvDigIncome=(TextView) view.findViewById(R.id.tv_dig_income);
        tvYesterdayDig=(TextView) view.findViewById(R.id.tv_yesterday_dig);
        tvOnline=(TextView) view.findViewById(R.id.tv_online);
        tvNet=(TextView) view.findViewById(R.id.tv_net);
        tvToday=(TextView)view.findViewById(R.id.tv_today);
        tvTime=(TextView)view.findViewById(R.id.tv_time);
        btnDig=(TextView)view.findViewById(R.id.btn_dig);
        btnDig.setOnClickListener(this);

        RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        adapter= new AdAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);

        RecyclerView onlineView = (RecyclerView)view.findViewById(R.id.onlineView);
        onlineView.setLayoutManager(new GridLayoutManager(getActivity(),6));
        onlineAdapter= new OnlineAdapter();
        onlineView.setAdapter(onlineAdapter);
        init();
        load();
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_dig:
                callBack.dig();
                break;
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Ad ad=(Ad) adapter.getData().get(position);
        jump(ad.getUrl());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden)init();
    }

    private void init(){
        User user=CacheUtils.getInstance(getContext()).getUser();
        if(user!=null)
            RetrofitUtils.getAPIService().index(user.getToken(),Const.DEVICE_TYPE).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseSubscriber<Index>() {
                @Override
                public void onSuccess(Index d) {
                    tvDigIncome.setText(d.getDig_income());
                    tvYesterdayDig.setText(d.getYesterday_dig());
                    tvToday.setText(d.getToday_dig_count()+"积分/天");
                    tvNet.setText(d.getUser_count());
                }
            });
    }
    private void load(){
        RetrofitUtils.getAPIService().ad().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseSubscriber<List<Ad>>() {
            @Override
            public void onSuccess(List<Ad> ads) {
                Log.d("aa","size:"+ads.size());
                adapter.addData(ads);
            }
        });
    }

    public void show(final String message,final boolean close){
        new AsyncTask<Object,Object,Object>(){
            @Override
            protected Object doInBackground(Object... objects) {
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                change(message,close);
                super.onPostExecute(o);
            }
        }.execute();
    }

    private void change(String message,boolean close){
        Log.d(TAG,message);
        if(close){
            btnDig.setSelected(false);
            btnDig.setText(R.string.start_dig);
        }else{
            btnDig.setSelected(true);
            btnDig.setText(R.string.diging);
        }
        if(close)return;
        if(!message.contains("|||"))message="0|||0|||0";
        final List<String> list= Splitter.on("|||").splitToList(message);
        tvOnline.setText(list.get(0));
        String time=list.get(1);
        if(TextUtils.isEmpty(time))time="0";
        tvTime.setText(time+"分钟");
        int count=Integer.parseInt(list.get(2));
        onlineAdapter.getData().clear();
        for(int i=1;i<13;i++) {
            onlineAdapter.addData((count>=i)?1:0);
        }

    }


    private void jump(String url){
        Intent intent = new Intent(getActivity(), WebActivity.class);
        intent.putExtra("url",url);
        getActivity().startActivity(intent);
    }

    ICallBack callBack;

    public void setCallBack(ICallBack callBack){
        this.callBack=callBack;
    }

}
